<template>
  <t-dialog
    :visible="visible"
    :header="currentTab === 'login' ? '登录' : '注册'"
    :width="500"
    :footer="false"
    @close="onClose"
  >
    <div class="login-container">
      <t-tabs v-model="currentTab" class="login-tabs">
        <t-tab-panel value="login" label="登录">
          <t-form style="margin-top: 20px;" ref="loginForm" :data="loginForm" @submit="onLoginSubmit">
            <t-form-item label="电话" name="email">
              <t-input
                v-model="loginForm.email"
                placeholder="请输入电话"
                :style="{ width: '100%' }"
              />
            </t-form-item>
            <t-form-item label="密码" name="password">
              <t-input
                v-model="loginForm.password"
                type="password"
                placeholder="请输入密码"
                :style="{ width: '100%' }"
              />
            </t-form-item>
            <div class="form-footer">
              <t-checkbox v-model="rememberMe">记住我</t-checkbox>
              <t-link theme="primary" hover="color">忘记密码？</t-link>
            </div>
            <t-button theme="primary" type="submit" block>登录</t-button>
          </t-form>
        </t-tab-panel>

        <t-tab-panel value="register" label="注册">
          <t-form style="margin-top: 20px;" ref="registerForm" :data="registerForm" @submit="onRegisterSubmit">
            <t-form-item label="电话" name="email">
              <t-input
                v-model="registerForm.email"
                placeholder="请输入电话"
                :style="{ width: '100%' }"
              />
            </t-form-item>
            <t-form-item label="用户名" name="username">
              <t-input
                v-model="registerForm.username"
                placeholder="请输入用户名"
                :style="{ width: '100%' }"
              />
            </t-form-item>
            <t-form-item label="密码" name="password">
              <t-input
                v-model="registerForm.password"
                type="password"
                placeholder="请输入密码"
                :style="{ width: '100%' }"
              />
            </t-form-item>
            <t-form-item label="确认密码" name="confirmPassword">
              <t-input
                v-model="registerForm.confirmPassword"
                type="password"
                placeholder="请再次输入密码"
                :style="{ width: '100%' }"
              />
            </t-form-item>
            <t-button style="margin-top: 20px;" theme="primary" type="submit" block>注册</t-button>
          </t-form>
        </t-tab-panel>
      </t-tabs>

      <div class="social-login">
        <div class="divider">
          <span>其他登录方式</span>
        </div>
        <div class="social-icons">
          <t-button variant="text" shape="square">
            <template #icon><t-icon name="logo-wechat" /></template>
          </t-button>
          <t-button variant="text" shape="square">
            <template #icon><t-icon name="logo-qq" /></template>
          </t-button>
          <t-button variant="text" shape="square">
            <template #icon><t-icon name="logo-weibo" /></template>
          </t-button>
        </div>
      </div>
    </div>
  </t-dialog>
</template>

<script>
export default {
  name: 'LoginDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  emits: ['update:visible', 'login-success'],
  data() {
    return {
      currentTab: 'login',
      rememberMe: false,
      loginForm: {
        email: '',
        password: ''
      },
      registerForm: {
        email: '',
        username: '',
        password: '',
        confirmPassword: ''
      }
    }
  },
  methods: {
    onClose() {
      this.$emit('update:visible', false)
    },
    onLoginSubmit({ validateResult, firstError }) {
      if (validateResult === true) {
        // TODO: 实现登录逻辑
        console.log('登录表单提交:', this.loginForm)
        this.$message.success('登录成功！')
        this.$emit('login-success')
        this.onClose()
      } else {
        console.log('登录表单校验失败:', firstError)
        this.$message.error(firstError)
      }
    },
    onRegisterSubmit({ validateResult, firstError }) {
      if (validateResult === true) {
        // TODO: 实现注册逻辑
        console.log('注册表单提交:', this.registerForm)
        this.$message.success('注册成功！')
        // 将注册的电话和密码传递到登录表单
        this.loginForm.email = this.registerForm.email
        this.loginForm.password = this.registerForm.password
        // 切换到登录标签页
        this.currentTab = 'login'
      } else {
        console.log('注册表单校验失败:', firstError)
        this.$message.error(firstError)
      }
    }
  }
}
</script>

<style scoped>
.login-container {
  padding: 20px;
}

.login-tabs {
  margin-bottom: 24px;
}

.form-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.social-login {
  margin-top: 32px;
}

.divider {
  display: flex;
  align-items: center;
  margin: 16px 0;
}

.divider::before,
.divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: #e7e7e7;
}

.divider span {
  padding: 0 16px;
  color: #999;
  font-size: 14px;
}

.social-icons {
  display: flex;
  justify-content: center;
  gap: 16px;
  margin-top: 16px;
}

[theme-mode="dark"] .divider::before,
[theme-mode="dark"] .divider::after {
  background: #333;
}

[theme-mode="dark"] .divider span {
  color: #666;
}

.contentLogin {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px 0;
}

.content {
  width: 100%;
  padding: 30px;
}

.top {
  width: 100%;
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 40px;
}

.left {
  height: 35vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  flex: 1;
  margin-right: 2vw;
}

.right {
  height: 35vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  flex: 1;
}

.title {
  font-size: clamp(20px, 1.2vw, 32px);
  font-weight: 500;
  margin-bottom: 4vh;
}

.button {
  margin-top: 6vh;
  width: 100%;
  height: 40px;
}

.qrcode-container {
  margin-top: 3vh;
  border: 1px solid #ccc;
  padding: 2vh;
  border-radius: 8px;
}

.imagelogin {
  width: 16vh;
  height: 16vh;
  min-width: 120px;
  min-height: 120px;
  max-width: 600px;
  max-height: 600px;
  object-fit: cover;
}

.input {
  margin-top: 3vh;
  width: 100%;
}

.input :deep(.t-input) {
  height: 40px;
  font-size: 16px;
}

.bottom {
  width: 100%;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  margin-top: 4vh;
  padding: 20px 2vw 0;
}

.leftimage {
  display: flex;
  gap: 2vw;
}

.righttext {
  font-size: clamp(14px, 1.1vw, 24px);
  color: var(--td-brand-color);
  cursor: pointer;
}

[theme-mode="dark"] .qrcode-container {
  border-color: #333;
}

@media (max-width: 500px) {
  .content {
    padding: 20px;
  }
  
  .left {
    margin-right: 0;
    height: auto;
    min-height: 300px;
  }

  .input {
    margin-top: 4vh;
  }

  .button {
    margin-top: 8vh;
  }

  .bottom {
    padding: 20px 0 0;
  }
}
</style> 